package com.example.supermarket.service.impl;

import com.example.supermarket.mapper.CustomerMapper;
import com.example.supermarket.pojo.*;
import com.example.supermarket.service.CustomerService;
import com.example.supermarket.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void createOrder(Order order) {
            Map<String,Object> map = ThreadLocalUtil.get();
            Integer userId=(Integer) map.get("userId");
            Customer customer=customerMapper.findByUserId(userId);
            Integer customerId=customer.getCustomerId();
            order.setCustomerId(customerId);
            customerMapper.createOrder(order);
    }

    @Override
    public void createLineItem(LineItem lineItem) {
        customerMapper.createLineItem(lineItem);
    }

    @Override
    public Customer getCustomerInfo() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("userId");
        return customerMapper.findByUserId(userId);
    }

    @Override
    public Integer findOrderId(Order order) {
        return customerMapper.findOrderId(order);
    }

    @Override
    public Order findCustomerOrder(Integer shopId) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("userId");
        Customer customer=customerMapper.findByUserId(userId);
        Integer customerId=customer.getCustomerId();
        return customerMapper.findCustomerOrder(customerId,shopId);
    }

    @Override
    public List<LineItem> findItemByOrderId(Integer orderId) {
        return customerMapper.findItemByOrderId(orderId);
    }

    @Override
    public Order findOrderByOrderId(Integer orderId) {
        return customerMapper.findOrderByOrderId(orderId);
    }

    @Override
    public void deleteOrder(Integer orderId) {
        customerMapper.deleteOrder(orderId);
    }

    @Override
    public void payOrder(Integer orderId) {
        customerMapper.updateOrderStatus(1,orderId);
    }

    @Override
    public List<Order> findHistoryOrder() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("userId");
        Customer customer=customerMapper.findByUserId(userId);
        Integer customerId=customer.getCustomerId();
        return customerMapper.findHistoryOrder(customerId);
    }

    @Override
    public Customer getCustomerByCustomerId(Integer customerId) {
        return customerMapper.getCustomerByCustomerId(customerId);
    }

}
